package com.sun.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

@RestController
public class TestApi {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/test")
    public String test() {
        //        redisTemplate.opsForHash().put("user_001", "age", 12);
//        redisTemplate.opsForHash().put("user_001", "name", "张三");
//        redisTemplate.opsForHash().put("user_001", "address", "SH");
//        redisTemplate.opsForHash().put("user_001", "email", "101412@qq.com");
//
//        Map user_001 = redisTemplate.opsForHash().entries("user_001");
//
//        List user_0011 = redisTemplate.opsForHash().values("user_001");
//        return user_0011.toString();
        redisTemplate.opsForSet().add("set", "第一个set元素");
        redisTemplate.opsForSet().add("set", "第二个set元素");
        redisTemplate.opsForSet().add("set", "第三个set元素");
        redisTemplate.opsForSet().add("set", "第四个set元素");
        redisTemplate.opsForSet().add("set", "第五个set元素");

        Set members = redisTemplate.opsForSet().members("set");

        redisTemplate.opsForSet().add("otherSet", "第一个set元素");
        redisTemplate.opsForSet().add("otherSet", "第二个set元素");
        redisTemplate.opsForSet().add("otherSet", "第三个set元素");
        redisTemplate.opsForSet().add("otherSet", "第四个set元素");

        //两个集合的交集
        Set intersect = redisTemplate.opsForSet().intersect("set", "otherSet");
        //并集
        Set union = redisTemplate.opsForSet().union("set", "otherSet");
        //差集
        Set difference = redisTemplate.opsForSet().difference("set", "otherSet");

        //做集合处理并存储在其他集合当中
        Long aLong = redisTemplate.opsForSet().differenceAndStore("set", "otherSet", "targetSet");
        //随机取出一个
        Object set = redisTemplate.opsForSet().randomMember("set");
        //随机取出两个
        List set1 = redisTemplate.opsForSet().randomMembers("set", 2);


        // 添加元素，有序集合按照score的值从小到大排序
        redisTemplate.opsForZSet().add("zset", "第一个zset", 100);
        redisTemplate.opsForZSet().add("zset", "第二个zset", 110);
        redisTemplate.opsForZSet().add("zset", "第三个zset", 120);
        redisTemplate.opsForZSet().add("zset", "第四个zset", 130);
        redisTemplate.opsForZSet().add("zset", "第五个zset", 140);

        // 获取元素在集合中的排名，按照score值从小到大排序
        Long rank = redisTemplate.opsForZSet().rank("zset", "第二个zset");
        // 获取元素在集合中的排名，按照score值从大到小排序
        Long aLong1 = redisTemplate.opsForZSet().reverseRank("zset", "第二个zset");

        // 获取集合中给定区间的元素，start开始位置，end结束位置，-1查询所有(从小到大)
        // 返回值为:Set<ZSetOperations.TypedTuple<V>>
        Iterator iterator = redisTemplate.opsForZSet().rangeWithScores("zset", 0, -1).iterator();
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple typedTuple = (ZSetOperations.TypedTuple) iterator.next();
            System.out.println("从小到大：" + typedTuple.getValue() + ", " + typedTuple.getScore());
        }
        return null;
    }
}
